package com.sy.simpleegg.room.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import com.sy.simpleegg.platform.BackstagePlatform
import com.sy.simpleegg.room.entity.CashPackageEntity

@Dao
interface CashPackageDao {
    @Query("SELECT * FROM tb_cash_package")
    fun getAll(): MutableList<CashPackageEntity>?

    @Query("SELECT COUNT(*) FROM tb_cash_package WHERE platform = :platform")
    fun getCashPackageCount(platform: BackstagePlatform): Long

    @Query("SELECT * FROM tb_cash_package WHERE platform = :platform")
    fun getCashPackageData(platform: BackstagePlatform): MutableList<CashPackageEntity>?

    @Query("SELECT COUNT(*) FROM tb_cash_package WHERE id = :id")
    fun getCount(id: Long): Long

//    @Query("SELECT * FROM tb_good WHERE platform = :platform ORDER BY id DESC LIMIT 1")
//    fun getLastGoods(platform: BackstagePlatform): MutableList<CashPackageEntity>?

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(goodEntity: CashPackageEntity)

    @Transaction
    fun insertAll(goodEntities: MutableList<CashPackageEntity>){
        goodEntities.forEach {
            if (getCount(it.id) > 0){
                update(it)
            }else {
                insert(it)
            }
        }
    }

    @Update
    fun update(goodEntity: CashPackageEntity)

    @Delete
    fun delete(goodEntity: CashPackageEntity)

    @Query("DELETE FROM tb_cash_package")
    fun deleteAll()
}